package com.boot.mybatis.handler;

import com.boot.commons.option.SexEnum;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * mybatis类型转换器,实现枚举查询自动转换
 */
@MappedTypes(SexEnum.class)
public class SexEnumHandler implements TypeHandler<SexEnum> {


    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, SexEnum sex, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i, sex.getCode());
    }

    @Override
    public SexEnum getResult(ResultSet resultSet, String s) throws SQLException {
        Integer code = resultSet.getInt(s);
        return SexEnum.getMsgByCode(code);
    }

    @Override
    public SexEnum getResult(ResultSet resultSet, int i) throws SQLException {
        Integer code = resultSet.getInt(i);
        return SexEnum.getMsgByCode(code);
    }

    @Override
    public SexEnum getResult(CallableStatement callableStatement, int i) throws SQLException {
        Integer code = callableStatement.getInt(i);
        return SexEnum.getMsgByCode(code);
    }


}
